package com.info.zhiduoduo.admin.service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.admin.model.formbean.project.ListSubProjectFormBean;
import com.info.zhiduoduo.admin.model.formbean.project.LongProjectListFormBean;
import com.info.zhiduoduo.common.constants.Constants;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.common.utils.DateUtil;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.dto.project.LongProjectBriefDto;
import com.info.zhiduoduo.repository.dto.project.LongProjectDto;
import com.info.zhiduoduo.repository.dto.project.ProjectBriefDto;
import com.info.zhiduoduo.repository.entity.order.TradeRecord;
import com.info.zhiduoduo.repository.entity.system.SystemProjectcategory;
import com.info.zhiduoduo.repository.mapper.company.CompanyProjectMapper;
import com.info.zhiduoduo.repository.mapper.company.LongTermProjectMapper;
import com.info.zhiduoduo.repository.mapper.system.ProjectCategoryMapper;

import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

@Service
public class LongProjectService extends BaseService {
    @Resource
    private LongTermProjectMapper longTermProjectMapper;
    @Resource
    private ProjectCategoryMapper projectCategoryMapper;
    @Resource
    private CompanyProjectMapper companyProjectMapper;
    /**
     * 获取项目详情
     */
    public SmartResult<LongProjectDto> getProjectDetail(BaseSimpleFormBean<Integer> formBean) {

        Integer projectid = formBean.getReqdata();
        if (projectid == null || projectid <= 0) return errorParamSmartResult();
        LongProjectDto longProjectDetail = longTermProjectMapper.getLongProjectDetail(projectid);
        //项目剩余天数
        Date now = new Date();
        if (longProjectDetail.getStartTime() != null) {
            int leftDays = DateUtil.getIntervalDays(now, longProjectDetail.getExpireDate());
            longProjectDetail.setLeftDays(leftDays);
        }
        //项目分类名
        SystemProjectcategory parentCategory = projectCategoryMapper.getParentCategory(longProjectDetail.getCategoryid());
        if (parentCategory != null) {
            longProjectDetail.setCategoryname(parentCategory.getProjectcategory());
        }
        //项目支付状态
        TradeRecord tradeRecord = getCompanyPayUserAmountTradeRecord(projectid);
        if (tradeRecord != null) {
            longProjectDetail.setTradeid(tradeRecord.getTradeid()).setTradestate(tradeRecord.getTradestate()).setRepay(tradeRecord.getRepay());
        }
        return smartResult(longProjectDetail);
    }

    public TradeRecord getCompanyPayUserAmountTradeRecord(int projectid) {
        return new TradeRecord().selectOne(Condition.create()
                .eq("projectid", projectid)
                .eq("business_type", Constants.KLT_PAY_BUSINESS_TYPE_USER_AMOUNT)
                .orderBy("tradeid", false)
                .last("limit 1"));
    }
    public TradeRecord getCompanyPayJiugeChargeTradeRecord(int projectid) {
        return new TradeRecord().selectOne(Condition.create()
                .eq("projectid", projectid)
                .eq("business_type", Constants.KLT_PAY_BUSINESS_TYPE_JIUGE_CHARGE)
                .orderBy("tradeid", false)
                .last("limit 1"));
    }


    /**
     * 获取长期项目列表
     */
    public SmartResult<ProjectBriefDto> getProjectList(BaseSimpleFormBean<LongProjectListFormBean> formBean) {

        LongProjectListFormBean reqdata = formBean.getReqdata();
        if (reqdata == null) return errorParamSmartResult();

        int compid = reqdata.getCompid();
        String name = reqdata.getName();
        int state = reqdata.getState();
        Date startDate = reqdata.getStartDate();
        Date endDate = reqdata.getEndDate();
        int categoryid = reqdata.getCategoryid();

        if (startDate == null) startDate = new Date((0));
        if (endDate == null) endDate = new Date();

        Page<ProjectBriefDto> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
        List<LongProjectBriefDto> list = longTermProjectMapper.getProjectBriefListByCompid(compid, 0, name, categoryid, state, startDate, endDate);

        //项目剩余天数
        Date now = new Date();
        list.forEach(p -> {
            Date expireDate = p.getExpireDate();
            if (p.getProjectstart() != null && p.getState() != 3) {
                p.setLeftDays(DateUtil.getIntervalDays(now, expireDate));
            }
        });

        return smartResultPage(page);
    }

   public SmartResult<ProjectBriefDto> listSubProjects(BaseSimpleFormBean<ListSubProjectFormBean> formBean) {
       @Valid ListSubProjectFormBean reqdata = formBean.getReqdata();
       Page<ProjectBriefDto> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
       companyProjectMapper.listLongTermSubProjects(reqdata.getParentProjectid(), reqdata.getState(), reqdata.getWhetherUploadFile());
       return SmartResult.ofPage(page);
   }
}
